<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../static/Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../static/Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../static/Css/style.css" />
    <link rel="stylesheet" type="text/css" href="../static/layui/css/layui.css"/>
    <script type="text/javascript" src="../static/Js/jquery.js"></script>
    <script type="text/javascript" src="../static/Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../static/Js/bootstrap.js"></script>
    <script type="text/javascript" src="../static/Js/ckform.js"></script>
    <script type="text/javascript" src="../static/Js/common.js"></script>
    <!-- <script type="text/javascript" src="../static/layui/layui.js"></script> -->
    <script type="text/javascript" src="../static/layer/layer.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
        th,td{
			text-align:center;
        }
        .sup{
        	margin: 0px 5px;
    		background-color: orangered;
    		border: none;
    		color: white;
        }
        .sup_div1{
            box-shadow: 0px 5px 5px rgba(0,0,0,0.1);
    		line-height: 50px;
    	}
    		
    	#supSearchInput{
    	    line-height: 30px;
    		width: 255px;
    		margin: 0px 15px;
    		text-indent: 15px;
    	}


    </style>
</head>
<body>
	<div class="sup_div1">
	    <input  type="text" id="supSearchInput" placeholder="输入供应商信息进行查询" class="abc input-default" placeholder="" value="">&nbsp;&nbsp;  
    	<button type="button" id="supSearchParams" class="btn btn-primary">查询</button>&nbsp;&nbsp; 
    	<button type="button" class="btn btn-success" id="addSupplier">新增供应商</button>
	</div>   
	
	<table class="table table-bordered table-hover definewidth m10" >
		<colgroup>
	    	<col width="20%">
	    	<col width="20%">
	    	<col width="20%">
	    	<col width="20%">
	    	<col width="15%">
	  	</colgroup>
    	<thead>
    		<tr>
		        <th>供应商名称</th>
		        <th>公司名称</th>
		        <th>公司地址</th>
		        <th>生成时间</th>
		        <th>操作</th>
	    	</tr>
	    </thead>
	    <tbody id="supTbody"></tbody>
	</table>
<!-- <div class="inline pull-right page">
         </div> -->
</body>
</html>
<script type="text/javascript">
	
	$(document).ready(function(){
		var searchContent = $("#supSearchInput").val();
		getSupplierList(searchContent);
	});
	
	function getSupplierList(searchContent){
		$.ajax({
			type:"post",
			dataType:"json",
			url:"/supplier/getSupplierList",
			data:{"content":searchContent},
			success:function(result){
				if(result.ret == 1){
					var sl = result.list;
					$("#supTbody").empty();
					for(var i = 0 ;i < sl.length; i++){
	                    var sup = sl[i];
	                    var str = "<tr class='sup_"+sup.id+"'>";
	                    str += "<td>"+sup.name+"</td>";
	                    str += "<td>"+sup.company+"</td>";
	                    str += "<td>"+sup.address+"</td>";
	                    var time = new Date(parseInt(sup.createDate)).toLocaleString().replace(/:\d{1,2}$/,' ');
	                    str += "<td class='time'>"+time+"</td>";
	                    str += "<td>";
	                    str += "<button value='"+sup.id+"' class='btn btn-primary sup supDel'>删除</button>";
	                    str += "<button value='"+sup.id+"' class='btn btn-primary ord ordEdit' style='background:#2196f3'>修改</button>";
	                    //str += "<button value='"+order.id+"' class='btn btn-primary ord downOrder' style='background:#3f51b5'>下单</button>";
	                    //<button type="button" id="cusSearchParams" class="btn btn-primary">查询</button>
	                    str += "</td>";
	                    str += "</tr>";
	                    $("#supTbody").append(str);
		    		}
				}else{
					ErrTips(result.msg);
				}
			},
			error:function(result){
				ErrTips("服务器错误");
			}
		})
	};
	
	var editIndex;
	var currentId = "";
	$(document).on('click','.ordEdit',function () {
		var supId = $(this).attr('value');
		currentId = supId;
		$.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/customer/getCustomerById",
	      	data:{"id":cusId},	
	      	success:function(result){
	            if(result.ret == 1){
	            	var cus = result.cus;
	                editIndex = layer.open({
	                    type: 1,
	                    title: '修改客户信息',
	                    content: "<div style='line-height: 20px;margin: 10px 20px;width: 300px;font-size: 13px'>" +
	                    "<span style='width: 80px'>姓名：</span><input value='"+cus.name+"' placeholder='联系人姓名' maxlength='10' id='cusConName' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span style='width: 80px'>电话：</span><input value='"+cus.phone+"' placeholder='联系人电话' oninput='if(value.length>11)value=value.slice(0,11)' id='cusConPhone' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='number'/></br> " +
	                    "<span style='width: 80px'>公司名称：</span><input value='"+cus.company+"' placeholder='公司名称' maxlength='25' id='cusName' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span style='width: 80px'>公司地址：</span><input value='"+cus.address+"' placeholder='公司地址' maxlength='50' id='cusAddress' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span id='cus_editSub' style='cursor: pointer;display: block;width: 80px;height: 30px;text-align: center;margin: 20px auto;background: cornflowerblue;line-height: 30px;color: white;border-radius: 5px'>确定</span>"+
	                    "</div>",
	                    anim: '1',
	                    style: 'position:fixed;left:0; padding:10px 0; border:none;top: 50px;margin: 20px;right: 0px;border-radius: 10px;'
	                });
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
	});
	
	
	$(document).on('click','#cus_editSub',function () {
		
        var name = $("#cusConName").val();
        var phone = $("#cusConPhone").val();
        var company = $("#cusName").val();
        var address = $("#cusAddress").val();
        
        if(currentId == ""){
        	ErrTips("id不能为空");
            return;
        }
        
        if($.trim(name) == "" || $.trim(phone) == "" || $.trim(company) == "" || $.trim(address) == ""){
        	ErrTips("有未填写的信息");
            return;
        }
        
        $.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/customer/updateCustomerById",
	      	data:{"name":name,"phone":phone,"company":company,"address":address,"id":currentId},	
	      	success:function(result){
	            if(result.ret == 1){
	            	TureTips(result.msg);
	            	layer.close(editIndex);
	            	$("#cusTbody").empty();
	            	getCustomerList();
	            	//setTimeout("location.reload()",1500);
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
    });
	
	$("#supSearchParams").on('click',function(){
		var searchContent = $("#supSearchInput").val();
		if($.trim(searchContent) == ""){
	   		ErrTips("搜索内容不能为空");
	        return;
	    }
		getSupplierList(searchContent);
		
	})
	
	var addIndex;
	$(document).on('click','#addSupplier',function () {
		//弹出层
        addIndex = layer.open({
            type: 1,
            title: '添加供应商',
            content: "<div style='line-height: 20px;margin: 10px 20px;width: 300px;font-size: 13px'>" +
            "<span style='width: 80px;display:inline-block;'>供应商名称：</span><input maxlength='25' id='supName' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
            "<span style='width: 80px;display:inline-block;'>公司名称：</span><input maxlength='30' id='company' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' /></br> " +
            "<span style='width: 80px;display:inline-block;'>公司地址：</span><input maxlength='40' id='address' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
            "<span id='sup_submit' style='cursor: pointer;display: block;width: 80px;height: 30px;text-align: center;margin: 20px auto;background: cornflowerblue;line-height: 30px;color: white;border-radius: 5px'>确定</span>"+
            "</div>",
            anim: '1',
            style: 'position:fixed;left:0; padding:10px 0; border:none;top: 50px;margin: 20px;right: 0px;border-radius: 10px;'
        });
	});
	
	$(document).on('click','#sup_submit',function () {
		var name = $("#supName").val();
		var company = $("#company").val();
		var address = $("#address").val();
		
		if($.trim(name) == "" || $.trim(company) == "" || $.trim(address) == ""){
			ErrTips("有未填写的信息");
			return;
		}
		
		$.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/supplier/insertSupplier",
	      	data:{"name":name,"company":company,"address":address},	
	      	success:function(result){
	            if(result.ret == 1){
	        		layer.close(addIndex);
	            	TureTips(result.msg);
	            	getSupplierList("");
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
	})
	
	$(document).on('click','.supDel',function () {
		var supId = $(this).attr('value');
		layer.confirm('确认删除吗？', {
			icon:3,
			btn: ['确定','取消'] //按钮
			}, function(){
				deleteSupplier(supId);
			});
	});
	
	function deleteSupplier(supId){
		$.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/supplier/deleteSupplier",
	      	data:{"id":supId},	
	      	success:function(result){
	            if(result.ret == 1){
	            	TureTips(result.msg);
	            	$(".sup_"+supId).remove();
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
	};


	function ErrTips(msg) {
		layer.msg(msg,{icon:7,time:1000});
    }
	
	function TureTips(msg) {
		layer.msg(msg,{icon:1,time:1500});
	}

</script>